﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CWCS.Core.Library.SocketManager;
using CWCS.Common;
using COMRW;

namespace CWCS.UIDesigner.DTSControl
{
    public class DTSControl 
    {
        public delegate void AnalyseMessageEventHandler(NotifyCommand command, object mesgs);//声明AnysisData解析收到数据时事件的代理，传给BLL层
        public event AnalyseMessageEventHandler EAnlyseData;//解析收到数据并传给BLL
        private DTSSocketClient DTSSocket = new DTSSocketClient();
        public string m_serverIP;//，字段名
        private int m_serverPort;
        private bool b_Connect;

        public bool B_Connect
        {
            get { return b_Connect; }
            set { b_Connect = value; }
        }
        public DTSControl(string ipaddress, int port)//构造函数 
        {
            m_serverIP = ipaddress;
            m_serverPort = port;
            DTSSocket.Notify += new DTSNotifyCommandHander(OnEAnlyseData);
        }


        public bool Connection() //建立Socket连接
        {
            try
            {
                if (DTSSocket.Connected)//如果连接上，先关闭，然后再连接
                    DTSSocket.Close();
                if (DTSSocket.Connect(m_serverIP, m_serverPort))
                {
                    return true;
                }
                else
                {
                    OnEAnlyseData(NotifyCommand.Break, "DTS Socket连接失败!");
                    return false;
                }

            }
            catch (Exception ex)
            {
                OnEAnlyseData(NotifyCommand.Break, " DTS Socket连接异常" + ex.Message);
                return false;
            }
        }

        public bool DisConnect()//关闭Socket连接
        {
            if (DTSSocket != null)
            {
                DTSSocket.Close();
                OnEAnlyseData(NotifyCommand.Break, " DTS Socket连接关闭!");
                return true;
            }
            else
            {
                OnEAnlyseData(NotifyCommand.Break, " DTS Socket连接关闭失败!");
                return false;
            }
        }

        public bool Send(string strMsg,int type)
        {
            try
            {
                //Byte[] sendBuffer = new byte[11];
                //sendBuffer = System.Text.Encoding.ASCII.GetBytes(strMsg.ToCharArray());
                //bool flag = DTSSocket.Send(sendBuffer);

                byte[] data = Encoding.UTF8.GetBytes(strMsg);
                byte[] bd = new byte[data.Length + 7];

                bd[0] = 0xfa;
                bd[1] = 0xfa;

                byte[] lengtht_bytes = BitConverter.GetBytes(data.Length);
                bd[6] = (byte)(type == 1 ? 0x01 : 0x04);
                Array.Copy(lengtht_bytes,0, bd,2, lengtht_bytes.Length);
                Array.Copy(data, 0, bd, 7, data.Length);
                bool flag = DTSSocket.Send(bd);

                return flag;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void OnEAnlyseData(NotifyCommand command, object mesgs) //收到数据触发报文解析事件
        {
            if (EAnlyseData != null)
            {
                EAnlyseData(command, mesgs.ToString());
            }
        }
    }
}
